package com.anbinh.doctor.fragment.account;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.DoctorInfoAdapter;
import com.anbinh.doctor.model.Articles;
import com.anbinh.doctor.model.Association;
import com.anbinh.doctor.model.Clinic;
import com.anbinh.doctor.model.Degree;
import com.anbinh.doctor.model.DoctorAppellation;
import com.anbinh.doctor.model.DoctorInfo;
import com.anbinh.doctor.model.DoctorPosition;
import com.anbinh.doctor.model.Language;
import com.anbinh.doctor.model.ResponExtraInfo;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.AccountType;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentDoctorInfoExtend extends Fragment {
	private static FragmentDoctorInfoExtend fragment = null;
	private View mView;
	private DoctorInfoAdapter adapter = null;
	private ListView mGridView;
	private CircleProgressBar loadding;
	private DoctorInfo currentDoctorInfo;

	public static FragmentDoctorInfoExtend newInstance() {
		if (fragment == null) {
			fragment = new FragmentDoctorInfoExtend();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_thongtincanhan, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mGridView = (ListView) mView.findViewById(R.id.listView1);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData();
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle = new Bundle();
				boolean isIntent = false;
				currentDoctorInfo = adapter.getItem(position);
				if (adapter.getItemViewType(position) == DoctorInfoAdapter.TYPE_SEPARATOR) {
					bundle.putInt(Utils.KEY, currentDoctorInfo.getRegId());
					bundle.putString(Utils.TITLE,
							"Thêm " + currentDoctorInfo.getTitle());
					isIntent = true;
				} else {

					switch (currentDoctorInfo.getSeparatorId()) {
					case R.id.chuc_vu:
						bundle.putInt(Utils.KEY, Utils.EDIT_CHUCVU);
						bundle.putString(
								Utils.TITLE,
								getResources().getString(
										R.string.cap_nhat_chuc_vu));
						isIntent = true;
						break;
					case R.id.bang_cap:
						bundle.putInt(Utils.KEY, Utils.EDIT_BANGCAP);
						bundle.putString(
								Utils.TITLE,
								getResources().getString(
										R.string.cap_nhat_bang_cap));
						isIntent = true;
						break;

					case R.id.hiep_hoi_chuyen_khoa:
						bundle.putInt(Utils.KEY, Utils.EDIT_HIEPHOI);
						bundle.putString(
								Utils.TITLE,
								getResources().getString(
										R.string.cap_nhat_chuyen_khoa));
						bundle.putInt(Utils.KEY_DOCTORID, -1);
						isIntent = true;
						break;
					case R.id.bai_viet:
						bundle.putInt(Utils.KEY,Utils.EDIT_BAIVIET);
						bundle.putString(
								Utils.TITLE,
								getResources().getString(
										R.string.cap_nhat_bai_viet));
						bundle.putInt(Utils.KEY_DOCTORID, -1);
						bundle.putBoolean(Utils.KEY_UPDATE, true);
						isIntent = true;
						break;

					}
				}
				if (isIntent) {
					bundle.putBoolean(Utils.CUSTOMBAR, true);
					bundle.putInt(Utils.REQUEST_CODE, Utils.RESUME_EXTRA_INFO);
					Utils.getNewIntent((Activity) getActivity(), bundle);
				}
			}

		});

	}

	private void gotoBrowser(String link) {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(link));
		startActivity(i);
	}

	public void onLoadData() {
		mGridView.setVisibility(View.INVISIBLE);
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		final UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		final int accountType = userLogin.getData().getUserInfo().getType();
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestGetExtraInfo(token,
				accountType, new OnRequest<ResponExtraInfo>() {

					@Override
					public void onSuccess(ResponExtraInfo data) {
						mGridView.setVisibility(View.VISIBLE);
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						if (getActivity() == null)
							return;

						if (data.getResponsecode() >= 0) {
							List<DoctorInfo> mListCate = null;
							if (accountType == AccountType.PHONGKHAM.getCode()) {
								mListCate = Utils.getTitleExend(getActivity(),
										R.menu.category_clinic_info_extend);
							} else {
								mListCate = Utils.getTitleExend(getActivity(),
										R.menu.category_doctor_info_extend);
							}

							if (adapter == null) {
								adapter = new DoctorInfoAdapter(getActivity(),
										new IRcallBak() {

											@Override
											public void onDisplay(int type,
													Bundle data) {
												onLoadData();
											}
										});
							} else {
								adapter.RemoveData();
							}
							for (int i = 0; i < mListCate.size(); i++) {
								DoctorInfo doctor = mListCate.get(i);
								int id = mListCate.get(i).getRegId();
								doctor.setSeparatorId(mListCate.get(i)
										.getRegId());
								adapter.addSeparatorItem(doctor);

								switch (id) {
								case R.id.phong_kham:
									ArrayList<Clinic> mListPk = data.getData()
											.getclinic_type();
									if (mListPk != null && mListPk.size() > 0) {
										for (int j = 0; j < mListPk.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListPk.get(j)
													.getName());
											adapter.addItem(item);
										}
									}
									break;
								case R.id.chuyen_khoa:
									ArrayList<String> mList = data.getData()
											.getSpecialities();
									if (mList != null && mList.size() > 0) {
										for (int j = 0; j < mList.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mList.get(j));
											adapter.addItem(item);
										}
									}
									break;
								case R.id.bang_cap:
									ArrayList<DoctorAppellation> mListBc = data
											.getData().getDoctor_appellation();
									if (mListBc != null && mListBc.size() > 0) {
										for (int j = 0; j < mListBc.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListBc.get(j)
													.getAppellation());
											item.setRegId(mListBc.get(j)
													.getId());

											adapter.addItem(item);
										}
									}
									break;
								case R.id.chuc_vu:
									ArrayList<DoctorPosition> mListCv = data
											.getData().getDoctor_position();
									if (mListCv != null && mListCv.size() > 0) {
										for (int j = 0; j < mListCv.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListCv.get(j)
													.getPosition());
											item.setDescription(mListCv.get(j)
													.getAddress());
											item.setRegId(mListCv.get(j)
													.getId());
											item.setDoctorPosition(mListCv
													.get(j));
											adapter.addItem(item);
										}
									}
									break;
								case R.id.chuc_danh:
									ArrayList<Degree> mListCd = data.getData()
											.getDegree();
									if (mListCd != null && mListCd.size() > 0) {
										for (int j = 0; j < mListCd.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListCd.get(j)
													.getName());
											item.setRegId(mListCd.get(j)
													.getId());
											adapter.addItem(item);
										}
									}
									break;
								case R.id.hiep_hoi_chuyen_khoa:
									ArrayList<Association> mListHhck = data
											.getData()
											.getDoctor_specialist_association();
									if (mListHhck != null
											&& mListHhck.size() > 0) {
										for (int j = 0; j < mListHhck.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListHhck
													.get(j)
													.getSpecialist_association());
											item.setRegId(mListHhck.get(j)
													.getId());
											adapter.addItem(item);
										}
									}
									break;

								case R.id.bai_viet:
									ArrayList<Articles> mListbv = data
											.getData().getDoctor_articles();
									if (mListbv != null && mListbv.size() > 0) {
										for (int j = 0; j < mListbv.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListbv.get(j)
													.getArticles());
											item.setRegId(mListbv.get(j)
													.getId());
											item.setLinks(mListbv.get(j)
													.getLinks());
											adapter.addItem(item);
										}
									}
									break;

								case R.id.ngon_ngu:
									ArrayList<Language> mListNN = data
											.getData().getLanguages();
									if (mListNN != null && mListNN.size() > 0) {
										for (int j = 0; j < mListNN.size(); j++) {
											DoctorInfo item = new DoctorInfo();
											item.setSeparatorId(id);
											item.setGroupName(mListCate.get(i)
													.getTitle());
											item.setTitle(mListNN.get(j)
													.getName());
											item.setLanguage(mListNN.get(j));
											item.setRegId(mListNN.get(j)
													.getId());
											adapter.addItem(item);
										}
									}
									break;
								}

							}
							mGridView.setAdapter(adapter);
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}
				});
	}

	public DoctorInfo getCurrentDoctorInfo() {
		return currentDoctorInfo;
	}

}
